﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace DATN.mds.ContactManagement
{
    public partial class CreateCM : System.Web.UI.Page
    {
        string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Us_Id"] != null)
                {
                    //Do something here!

                    //Note Remind
                    Session["Remind"] = null;
                }
                else
                {
                    Response.Redirect("../Account/Login.aspx");
                }
                ddr();
                Validate();
            }
        }

        //Load DropDownlist
        public void ddr() {

            //gioi tinh
            var gioitinh = new Dictionary<string, string>();
            gioitinh.Add("1", "Nam");
            gioitinh.Add("2", "Nữ");
            gioitinh.Add("3", "Khác");

            ddp_sex.DataTextField = "Value";
            ddp_sex.DataValueField = "Key";
            ddp_sex.DataSource = gioitinh;
            ddp_sex.DataBind();

            //Tinh trang hon nhan
            var Masta = new Dictionary<string, string>();
            Masta.Add("1", "Độc Thân");
            Masta.Add("2", "Đã Kết Hôn");
            Masta.Add("3", "Khác");

            ddp_MaStatus.DataTextField = "Value";
            ddp_MaStatus.DataValueField = "Key";
            ddp_MaStatus.DataSource = Masta;
            ddp_MaStatus.DataBind();

            //Tinh trang hon nhan
            var City = new Dictionary<string, string>();
            City.Add("1", "Hà Nội");
            City.Add("2", "Hưng yên");
            City.Add("3", "Điện Biên");
            City.Add("4", "Nam Định");

            ddp_City.DataTextField = "Value";
            ddp_City.DataValueField = "Key";
            ddp_City.DataSource = City;
            ddp_City.DataBind();

            //Cong viec
            var congviec = new Dictionary<string, string>();
            congviec.Add("1", "Kinh doanh");
            congviec.Add("2", "Văn phòng");
            congviec.Add("3", "Kĩ thuật");
            congviec.Add("4", "Học Sinh, Sinh Viên");
            congviec.Add("5", "Nhà Nước");
            congviec.Add("6", "Nông dân, Người lao động");

            ddp_Job.DataTextField = "Value";
            ddp_Job.DataValueField = "Key";
            ddp_Job.DataSource = congviec;
            ddp_Job.DataBind();
        }

        //Click Btt Save
        protected void BtSave_Click(object sender, EventArgs e)
        {
            if (Session["Us_Id"] != null)
            {
                //Do something here!
                int us_id = Convert.ToInt16(Session["Us_Id"].ToString());

                if (TbxFirstName.Text != null && TbxFirstName.Text != "")
                {
                    if (TbxLastName.Text != null && TbxLastName.Text != "")
                    {
                        if (TbxBirthday.Text != null && TbxBirthday.Text != "")
                        {
                            try
                            {
                                int Salary = Convert.ToInt16(TbxSalary.Text);
                                try {
                                    DateTime date = Convert.ToDateTime(TbxBirthday.Text);
                                    // lưu thông tin khách hàng:
                                    SaveCM(us_id);
                                }
                                catch (Exception ex)
                                {
                                    LbStatus.Text += "Nhập 'Ngày Sinh' chưa đúng!" + "<br/>";
                                }
                            }
                            catch (Exception ex)
                            {
                                LbStatus.Text += "Nhập 'Mức Lương' chưa đúng!" + "<br/>";
                            }
                        }
                        else
                        {
                            LbStatus.Text += "Chưa nhập ngày sinh!" + "<br/>";
                        }
                    }
                    else
                    {
                        LbStatus.Text += "Chưa nhập vào ô họ!" + "<br/>";
                    }
                }
                else
                {
                    LbStatus.Text += "Chưa nhập vào ô tên!" + "<br/>";
                }
            }
            else
            {
                Response.Redirect("../Account/Login.aspx");
            }
        }

        //Saved Contact
        protected void SaveCM(int us_id)
        {
            try
            {
                int Salary = 0;
                Salary = Convert.ToInt16(TbxSalary.Text.ToString());
            
                
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();
                cmd = cnn.CreateCommand();
                cmd.CommandText = "INSERT INTO [dbo].[Customers] ([FirstName], [LastName], [Birthday], [Sex], "
                    + "[Phone], [Mail], [Address], [City], [Company], [MaritalStatus], "
                    + "[Job], [Salary], [Habit], [DateCreate], [Us_Id]) "
                    + "VALUES (@FirstName, @LastName, @Birthday, @Sex, @Phone, @Mail, @Address, @City,"
                    + "@Company, @MaritalStatus, @Job, @Salary, @Habit, @DateCreate,"
                    + "@Us_Id)";

                cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@Birthday", SqlDbType.Date);
                cmd.Parameters.Add("@Sex", SqlDbType.NVarChar, 20);
                cmd.Parameters.Add("@Phone", SqlDbType.VarChar, 16);
                cmd.Parameters.Add("@Mail", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar, 150);
                cmd.Parameters.Add("@City", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@Company", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@MaritalStatus", SqlDbType.NVarChar, 20);
                cmd.Parameters.Add("@Job", SqlDbType.NVarChar, 50);
                cmd.Parameters.Add("@Salary", SqlDbType.Int);
                cmd.Parameters.Add("@Habit", SqlDbType.NText);
                cmd.Parameters.Add("@DateCreate", SqlDbType.DateTime);
                // cmd.Parameters.Add("@GrCus_Id", SqlDbType.Int);
                cmd.Parameters.Add("@Us_Id", SqlDbType.Int);

                cmd.Parameters["@FirstName"].Value = TbxFirstName.Text;
                cmd.Parameters["@LastName"].Value = TbxLastName.Text;
                cmd.Parameters["@Birthday"].Value = TbxBirthday.Text;
                cmd.Parameters["@Sex"].Value = ddp_sex.SelectedValue.ToString();
                cmd.Parameters["@Phone"].Value = TbxPhone.Text;
                cmd.Parameters["@Mail"].Value = TbxMail.Text;
                cmd.Parameters["@Address"].Value = TbxAddress.Text;
                cmd.Parameters["@City"].Value = ddp_City.SelectedValue.ToString();
                cmd.Parameters["@Company"].Value = TbxCompany.Text;
                cmd.Parameters["@MaritalStatus"].Value = ddp_MaStatus.SelectedValue.ToString();
                cmd.Parameters["@Job"].Value = ddp_Job.SelectedValue.ToString();
                cmd.Parameters["@Salary"].Value = Salary;
                cmd.Parameters["@Habit"].Value = TbxHabits.Text.ToString();
                cmd.Parameters["@DateCreate"].Value = DateTime.Now.ToString();
                //cmd.Parameters["@GrCus_Id"].Value = Convert.ToInt16(ddp_dataviewCustomers.SelectedValue.ToString());
                cmd.Parameters["@Us_Id"].Value = us_id;
                cmd.ExecuteNonQuery();

                TbxAddress.Enabled = false;
                TbxBirthday.Enabled = false;
                TbxCompany.Enabled = false;
                TbxFirstName.Enabled = false;
                TbxHabits.Enabled = false;
                TbxLastName.Enabled = false;
                TbxMail.Enabled = false;
                TbxPhone.Enabled = false;
                TbxSalary.Enabled = false;
                BtSave.Enabled = false;
                BtCancel.Text = "Tạo mới";

                LbStatus.Text = "Đã tạo đc khách hàng mới!";
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
                cnn.Close();
            }
        }

        //Clear text
        protected void BtCancel_Click(object sender, EventArgs e)
        {
            TbxAddress.Text = "";
            TbxBirthday.Text = "";
            TbxCompany.Text = "";
            TbxHabits.Text = "";
            TbxMail.Text = "";
            TbxFirstName.Text = "";
            TbxLastName.Text = "";
            TbxPhone.Text = "";
            TbxSalary.Text = "";

            TbxAddress.Enabled = true;
            TbxBirthday.Enabled = true;
            TbxCompany.Enabled = true;
            TbxFirstName.Enabled = true;
            TbxHabits.Enabled = true;
            TbxLastName.Enabled = true;
            TbxMail.Enabled = true;
            TbxPhone.Enabled = true;
            TbxSalary.Enabled = true;
            BtSave.Enabled = true;
            BtCancel.Text = "Hủy Bỏ";
        }

        protected void btReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/mds.ContactManagement/IndexCM.aspx");
        }


    }
}